<?
if (preg_match("#" . basename(__FILE__) . "#", $_SERVER['PHP_SELF'])) {
	die('This page cannot be called directly.') ;
}

require_once 'includes/AjaxRes.php'; //System Debug...
class ajax_locationpage {

    function playme(){
        $code = Url::getParam('code');
        //yêu cầu đặt tên $code=tên của method trong class

        if (method_exists($this, $code)) {
            $this->$code();
        }
        else
        {
            $this->home();
        }
    }

    function home(){
        global $display;
        die("Nothing to do 1212...");
    }
	
	function paging_location(){
        global $display;
        $ajax_response = new AjaxRes();
        $page= (int)Url::get("page");
        $locationid = (int)Url::get('locationId');
        $star = (int)Url::get('star');
        if($page == 1){
            $offset = 0;
        }else{
            $offset = ($page-1)*10;
        }
        if($star == 0){
            $hotels = Db_Hotel::listHotelByLocation($locationid,$offset,10);
        }else{
            $hotels = Db_Hotel::listHotelLocationByStar($locationid,$star,$offset,10);
        }

        $dataAll = self::getDataHotel($hotels,$locationid);
        $totalshow =   sizeof($dataAll['hotelShow']);
        if($totalshow >0){
            if($star == 0){
                $total = Db_Hotel::total('city_id = '.$locationid.' AND status =1');
            }else{
                $total = Db_Hotel::total('city_id = '.$locationid.' AND status =1 AND star ='.$star );
            }
            $display->add('paging', ECPaging::pagingLocation($limit, $total, 10, 3, 'page_no',true, 'Khách sạn','',$page));
            $display->add('imgs',$dataAll['imgs']);
            $display->add('districtLoLike',$dataAll['districs']);
            $display->add('hotelLoLike', $dataAll['hotelShow']);
            $display->add('total', $totalshow);
            $display->add('city',$dataAll['citys']);
            $display->add('star',$star);
            $item = $display->output('paging', true, 'Location');
            $ajax_response->type = AjaxRes::SUCCESS;
            $ajax_response->item = $item;
            echo $ajax_response->toString();
        }else{
            $ajax_response->type = AjaxRes::SUCCESS;
            $ajax_response->message = "Không tìm được data";
            $ajax_response->item = '';
            echo $ajax_response->toString();
        }
	}

    function hotel_star_location(){
        global $display;
        $ajax_response = new AjaxRes();
        $page= (int)Url::get("page");
        $locationid = (int)Url::get('locationId');
        $star = (int)Url::get('star');
        if($page == 1){
            $offset = 0;
        }else{
            $offset = ($page-1)*10;
        }
        if($star ==0){
            $hotels = Db_Hotel::listHotelByLocation($locationid,$offset,10);
        }else{
            $hotels = Db_Hotel::listHotelLocationByStar($locationid,$star,$offset,10);
        }
        //var_dump($hotels);
        $dataAll = self::getDataHotel($hotels,$locationid);
        $totalshow =   sizeof($dataAll['hotelShow']);
        if($totalshow >0){
            if($star == 0){
                $total = Db_Hotel::total('city_id = '.$locationid.' AND status =1');
            }else{
                $total = Db_Hotel::total('city_id = '.$locationid.' AND status =1 AND star ='.$star );
            }

            $display->add('paging', ECPaging::pagingLocation($limit, $total,10, 3, 'page_no',true, 'Khách sạn','',$page));
            $display->add('imgs',$dataAll['imgs']);
            $display->add('districtLoLike',$dataAll['districs']);
            $display->add('hotelLoLike', $dataAll['hotelShow']);
            $display->add('total', $totalshow);
            $display->add('city',$dataAll['citys']);
            $display->add('star',$star);
            $item = $display->output('paging', true, 'Location');
            $ajax_response->type = AjaxRes::SUCCESS;
            $ajax_response->item = $item;
            echo $ajax_response->toString();
        }else{
            $ajax_response->type = AjaxRes::SUCCESS;
            $ajax_response->message = "Không tìm được data";
            $ajax_response->item = '';
            echo $ajax_response->toString();
        }
    }

    /*function getDataHotel($hotels,$cityId){
        $imgs = array();
        $citys = array();
        $imgsDefault = array();
        $hotelShow = array();
        $districs = array();
        // $now  = strtotime(date("d-m-Y").' 00:00:00');
        $disticsOfCity = CGlobal::$all_districs[$cityId];
        foreach($hotels as $h){
            if($h['price_min'] !== 0 && $h['status'] == 1){
                $h['feel'] = Db_HotelReview::getFeelByScore($h['rating_0_0_average']);
                // $h['total_review'] = Db_HotelReview::total('hotel_id ='.$h['id'].' AND status =1');
                $h['total_review'] = Db_HotelReview::countTotalReviewByHotelId($h['id']);
                if(($temp= Db_HotelReview::getNewReviewHotel($h['id']))!== null){
                    $h['new_review']= String::display_sort_title($temp['comment'],30) ;
                }
                $hotelShow[] = $h;
                $citys[]=CGlobal::$all_cities[$h['city_id']];
                $districs[] = $disticsOfCity[$h['district_id']];
                if(REDIS_ON == 1){
                    $imgs[$h['image_default']] = Db_Image::getImageByIdFromRedis($h['image_default']);
                }else{
                    $imgsDefault[] =$h['image_default'];
                }
            }
        }

        if(sizeof($imgsDefault)>0){
            $imgsDefault = join(',',$imgsDefault);
            $imgs =Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN ($imgsDefault)");
        }
        //var_dump($imgs);die;
        $data = array(
            'hotelShow'=>$hotelShow,
            'citys'=>$citys,
            'districs'=>$districs,
            'imgs'=>$imgs,
        );
        return $data;
    }*/

    function getDataHotel($hotels,$cityId){
        $imgs = array();
        $citys = array();
        $imgsDefault = array();
        $hotelShow = array();
        $districs = array();
        // $now  = strtotime(date("d-m-Y").' 00:00:00');
        Db_Location::get_cities();
        if(isset(CGlobal::$all_districs[$cityId])){
            $disticsOfCity = CGlobal::$all_districs[$cityId];
            foreach($hotels as $h){
                $imgsDefault[] =$h['image_default'];
                if($h['price_min'] !== 0 && $h['status'] == 1){
                    $h['feel'] = Db_HotelReview::getFeelByScore($h['rating_0_0_average']);
                    $h['total_review'] = Db_HotelReview::total('hotel_id ='.$h['id'].' AND status =1');
                    if(($temp= Db_HotelReview::getNewReviewHotel($h['id']))!== null){
                        $h['new_review']=String::display_sort_title($temp['comment'],30) ;;
                    }
                    $hotelShow[] = $h;
                    $citys[]=CGlobal::$all_cities[$h['city_id']];
                    $districs[] = $disticsOfCity[$h['district_id']];
                }
            }
            if(sizeof($imgsDefault)>0){
                $imgsDefault = join(',',$imgsDefault);
                $imgs =Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN ($imgsDefault)");
            }
            $data = array(
                'hotelShow'=>$hotelShow,
                'citys'=>$citys,
                'districs'=>$districs,
                'imgs'=>$imgs,
            );
            return $data;
        }else{
            return false;
        }

    }

    function del_locations_selected(){
		if(!User::is_login()){
			echo "not_login";
			exit;
		}
        if(User::have_permit("location_admin")==false){
            echo "no_perm";
            exit;
        }
        
		$location_ids= Url::get("selected_ids");
		if(is_array($location_ids)){
            if(!Db_Location::delLocations($location_ids)){
                echo "un_success";
            }else{
                echo "success";
            }
		}else{
            echo "not_item";
        }
	}
}
?>